package com.boc.mainmodule.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.boc.mainmodule.R;
import com.boc.mainmodule.R2;
import com.boc.mainmodule.fragment.HomeRootFragment;

import butterknife.BindView;
import demo.bocweb.com.sdk.base.activity.BaseActivity;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.config.AppConfig;
import demo.bocweb.com.sdk.util.ResourcesUtils;
import demo.bocweb.com.sdk.util.SP;

import static demo.bocweb.com.sdk.config.ARoutConfig.homeActivityPath;

/**
 * 首页
 * Created by zhelun on 2018/2/7.
 */
@Route(path = homeActivityPath)
public class HomeActivity extends BaseActivity {
    @BindView(R2.id.fl)
    FrameLayout fl;
    @BindView(R2.id.rb_home)
    RadioButton rbHome;
    @BindView(R2.id.rb_goods)
    RadioButton rbGoods;
    @BindView(R2.id.rb_shopping)
    RadioButton rbShopping;
    @BindView(R2.id.rb_user)
    RadioButton rbUser;
    @BindView(R2.id.rg_main)
    RadioGroup rgMain;
    private Fragment homeFragment;
    private Fragment goodsFragment;
    private Fragment shoppingFragment;
    private Fragment userFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initData() {
        super.initData();
        setDefaultFragment();
        rbHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTabSelect();
                setTabSelect(rbHome);
                switchFragment(0);
            }
        });
        rbGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTabSelect();
                setTabSelect(rbGoods);
                switchFragment(1);
            }
        });
        rbShopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SP.contains(AppConfig.Authorization)) {
                    ARouter.getInstance().build(ARoutConfig.loginPath).greenChannel().navigation();
                } else {
                    resetTabSelect();
                    setTabSelect(rbShopping);
                    switchFragment(2);
                }
            }
        });
        rbUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SP.contains(AppConfig.Authorization)) {
                    ARouter.getInstance().build(ARoutConfig.loginPath).greenChannel().navigation();
                } else {
                    resetTabSelect();
                    setTabSelect(rbUser);
                    switchFragment(3);
                }
            }
        });

    }

    private void resetTabSelect() {
        rbHome.setTextColor(ResourcesUtils.getColor(R.color.black));
        rbGoods.setTextColor(ResourcesUtils.getColor(R.color.black));
        rbShopping.setTextColor(ResourcesUtils.getColor(R.color.black));
        rbUser.setTextColor(ResourcesUtils.getColor(R.color.black));
    }

    private void setTabSelect(RadioButton rb) {
        rb.setTextColor(ResourcesUtils.getColor(R.color.blue));

    }

    private void switchFragment(int position) {
        FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            case 0:
                hideFragment(beginTransaction);
                if (homeFragment == null) {
                    homeFragment = (Fragment) ARouter.getInstance().build(ARoutConfig.homeRootFragPath).navigation();
                    beginTransaction.add(R.id.fl, homeFragment);
                } else {
                    beginTransaction.show(homeFragment);
                }
                break;
            case 1:
                hideFragment(beginTransaction);

                if (goodsFragment == null) {
                    goodsFragment = (Fragment) ARouter.getInstance().build(ARoutConfig.goodsRootFragPath).navigation();
                    beginTransaction.add(R.id.fl, goodsFragment);
                } else {
                    beginTransaction.show(goodsFragment);
                }
                break;
            case 2:
                hideFragment(beginTransaction);
                if (shoppingFragment == null) {
                    shoppingFragment = (Fragment) ARouter.getInstance().build(ARoutConfig.shoppingRootFragPath).navigation();
                    beginTransaction.add(R.id.fl, shoppingFragment);
                } else {
                    beginTransaction.show(shoppingFragment);
                }
                break;
            case 3:
                hideFragment(beginTransaction);
                if (userFragment == null) {
                    userFragment = (Fragment) ARouter.getInstance().build(ARoutConfig.userRootFragPath).navigation();
                    beginTransaction.add(R.id.fl, userFragment);
                } else {
                    beginTransaction.show(userFragment);
                }
                break;
        }
        beginTransaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (goodsFragment != null) {
            transaction.hide(goodsFragment);
        }
        if (shoppingFragment != null) {
            transaction.hide(shoppingFragment);
        }
        if (userFragment != null) {
            transaction.hide(userFragment);
        }


    }

    private void setDefaultFragment() {
        resetTabSelect();
        setTabSelect(rbHome);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        homeFragment = new HomeRootFragment();
        transaction.add(R.id.fl, homeFragment).commit();

    }
}
